package com.gugee.ins.data.task.mark.service;

import com.gugee.ins.common.constant.InsRankTaskTypeCodeEnum;
import com.gugee.ins.common.dto.RankTaskParam;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerCfsAvgDao;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerCfsDao;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;


/**
 * ins同分类同量级估算排名任务
 * @author huangxin
 */
@Component
@Slf4j
public class InsStBloggerCfsSvc extends AbstractMarkRankProducerHandler {

    /**
     *
     */
    @Autowired
    InsStBloggerCfsDao insStBloggerCfsDao;

    /**
     *
     */
    @Autowired
    InsStBloggerCfsAvgDao insStBloggerCfsAvgDao;

    /**
     * 执行
     */
    @Override
    public void handleRank(RankTaskParam taskParam) {

        // 查找一级分类
        List<String> categoryList = getCategory(taskParam.getCategory(),false);

        // 获取指定的粉丝量级id信息
        List<Integer> followerSizeList = getFollowerSize(taskParam.getFollowerSize());

        // 当前时间
        long now = getDateTime(taskParam.getDateCode()) / 1000;

        long mediaLimitTime = now - taskConfig.getLatestDays() * 24 * 3600;
        if(followerSizeList != null && !followerSizeList.isEmpty() && categoryList != null && !categoryList.isEmpty()){
            for(int fs : followerSizeList){
                for(String category : categoryList){
                    int total = insBloggerMainDao.findTotalCountByCategoryAndFollowerSize(category,fs);
                    total = total > 0 ? total : 1;
                    if(taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.DEFAULT.getCode()
                            || taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.ST_CFS.getCode()){
                        // 更新同分类同量级估值表(这里改成用存储过程，连接超时也会执行成功)
                        updateStBloggerCfs(category,fs,total,now);
                    }
                    if(taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.DEFAULT.getCode()
                            || taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.ST_CFS_AVG.getCode()){
                        // 更新同分类同量级平均值表
                        updateStBloggerCfsAvg(category,fs,now);
                    }
                    if(taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.DEFAULT.getCode()
                            || taskParam.getTypeCode() == InsRankTaskTypeCodeEnum.ST_CFS_AD_AVG.getCode()){
                        // 更新同分类同量级广告帖子相关项平均值表
                        updateStBloggerCfsAdAvg(category,fs,mediaLimitTime,now);
                    }
                }
            }
        }
    }

    /**
     * 更新同分类同量级估值表
     * @param category
     * @param fs
     * @param total
     * @param now
     */
    public void updateStBloggerCfs(String category,int fs,int total,long now){
        try{
            // 更新同分类同量级估值表(这里改成用存储过程，连接超时也会执行成功)
            insStBloggerCfsDao.updateStBloggerCfsByProcedure(category,fs,total,now);
        }catch (Exception e){
            log.info("ins 更新同分类同量级数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
            XxlJobLogger.log("ins 更新同分类同量级数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
        }
    }

    /**
     * 更新同分类同量级平均值表
     * @param category
     * @param fs
     * @param now
     */
    public void updateStBloggerCfsAvg(String category,int fs,long now){
        try{
            // 更新同分类同量级平均值表
            insStBloggerCfsAvgDao.updateStBloggerCfsAvg(category,fs,now);
        }catch (Exception e){
            log.info("ins 更新同分类同量级平均值数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
            XxlJobLogger.log("ins 更新同分类同量级数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
        }
    }

    /**
     * 更新同分类同量级广告帖子相关项平均值表
     * @param category
     * @param fs
     * @param mediaLimitTime
     * @param now
     */
    public void updateStBloggerCfsAdAvg(String category,int fs,long mediaLimitTime,long now){
        try{
            // 更新同分类同量级广告帖子相关项平均值表
            insStBloggerCfsAvgDao.updateStBloggerCfsAdAvg(category,fs,mediaLimitTime,now);
        }catch (Exception e){
            log.info("ins 更新同分类同量级广告帖子相关项平均值数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
            XxlJobLogger.log("ins 更新同分类同量级广告帖子相关项数据 失败 ,fs:{},category:{}, msg :{}",fs,category, e.getMessage());
        }
    }
}
